#encoding:utf-8
import pandas as pd
from io import StringIO
from sklearn import datasets #导入数据集
from sklearn import linear_model
import numpy

from suds.client import Client
import  pypyodbc
import threading
from collections import deque
from PointInfo import PointInfo
#from suds.transport.https import HttpAuthenticated
import ConfigParser

cf = ConfigParser.ConfigParser()
cf.read("config.ini")
sqlserverdb_host = cf.get("sqlserver", "db.host")
sqlserverdb_user = cf.get("sqlserver", "db.user")
sqlserverdb_pass = cf.get("sqlserver", "db.pass")
sqlserverdb_defaultdb = cf.get("sqlserver", "db.database")
user_url= cf.get("WSrul", "db.host")
#iris=datasets.load_iris()  #加载数据集
#print(iris.data.shape)
#print iris.data
#print iris.target

'''from sklearn import svm #建立线性SVM分类器
clf=svm.LinearSVC()#建立线性SVM分类器
clf.fit(iris.data,iris.target)# 用数据训练模型
clf.predict([[8.0,8.2,8.3,8.6]])#训练好模型之后，输入新的数据进行预测
print clf

print clf.coef_  #训练好模型的参数'''

'''
通过 WS接口读取时间区间值
'''
def ReadData(client,StartTime,EndTime,kks):
    namesArr=[]
    namesArr.append(kks)
    instantDatass=client.service.getIntervalData(1,1,2,0,"%Ld"%StartTime,"%Ld"%EndTime,namesArr)
    if len(instantDatass)!=0:
        return instantDatass[0].detailArr
    else:
        return []

client=Client(user_url)
namesArr="GZ104HP4MKC64ME005BV11T8AC002AA05"

list1=[]#[150,200,250,300,350,400,600]
list2=[]#[6450,7450,8450,9450,11450,15450,18450]
datas=ReadData(client,1546594031000,1546680431000,namesArr)
print len(datas)
for data in datas:
    list1.append(data.value)
    list2.append(data.timestamp)
    #print data.value,data.time.timestamp

regr = linear_model.LinearRegression()
regr.fit(numpy.array(list1).reshape(-1, 1), numpy.array(list2))
a,b = regr.coef_,regr.intercept_
print a,b
area = 50

# 方式1：根据直线方程计算的价格
print(a * area + b)